package weaver.interfaces.zhlx.duss.action.purchasemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 固定资产购置-非项目下固资流程调用SAP采购需求新增接口（MM008）
 * @ModifyLog:
 **/
public class WeaGdzcgzFxmxgzlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaGdzcgzFxmxgzlcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("SERIAL", lcbh);
            dataJson.put("OANUM", requestid);
            JSONArray itemList = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject itemJson = new JSONObject();
                itemJson.put("BNFPO", Util.null2String(rs.getString("cgsqdxmbh")));
                itemJson.put("BSART", Util.null2String(rs.getString("cgsqpzlx")));
                itemJson.put("FRGKZ", Util.null2String(rs.getString("pzbs")));
                itemJson.put("EKGRP", Util.null2String(rs.getString("cgz")));
                itemJson.put("ERNAM", Util.null2String(rs.getString("cjdxdrymc")));
                itemJson.put("ERDAT", Util.null2String(rs.getString("zhggdrq")).replace("-", ""));
                itemJson.put("AFNAM", Util.null2String(rs.getString("xqz")));
                itemJson.put("TXZ01", Util.null2String(rs.getString("dwb")));
                itemJson.put("MATNR", Util.null2String(rs.getString("wlbh")));
                itemJson.put("WERKS", Util.null2String(rs.getString("gc")));
                itemJson.put("LGORT", Util.null2String(rs.getString("kcdd")));
                itemJson.put("BEDNR", Util.null2String(rs.getString("xqgzh")));
                itemJson.put("MATKL", Util.null2String(rs.getString("wlz")));
                itemJson.put("MENGE", Util.null2String(rs.getString("cgsqsl")));
                itemJson.put("MEINS", Util.null2String(rs.getString("cgsqjldw")));
                itemJson.put("LFDAT", Util.null2String(rs.getString("xmjhrq")).replace("-", ""));
                itemJson.put("PREIS", Util.null2String(rs.getString("cgsqzdjg")));
                itemJson.put("PEINH", Util.null2String(rs.getString("jgdw")));
                itemJson.put("PSTYP", Util.null2String(rs.getString("cgpzzdxmlb")));
                itemJson.put("KNTTP", Util.null2String(rs.getString("kmfplb")));
                itemJson.put("EKORG", Util.null2String(rs.getString("cgzz")));
                itemJson.put("KOSTL", Util.null2String(rs.getString("cbzx")));
                itemJson.put("DISUB_PSPNR", Util.null2String(rs.getString("wbsxmh")));
                itemList.add(itemJson);
            }
            dataJson.put("ITEM", itemList);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaGdzcgzFxmxgzlcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaGdzcgzFxmxgzlcAction>>>>>dataJson=" + dataJson);
            JSONObject resultJson = sapUtil.handleOKHttpRequest("MM008", dataJson, dataMap);
            writeLog(">>>>>WeaGdzcgzFxmxgzlcAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String dataStr = outputJson.getString("DATA");
                JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                JSONArray returnList = JSONArray.parseArray(returnDataJson.getString("RETURN"));
                if ("S".equals(msgType)) {
                    String msgText2 = "";
                    for (int i = 0; i < returnList.size(); i++) {
                        String msgType2 = returnList.getJSONObject(i).getString("MSG_TYPE");
                        if ("S".equalsIgnoreCase(msgType2)) {
                            msgText2 = returnList.getJSONObject(i).getString("MSG_TEXT2");
                            break;
                        }
                    }
                    String sql = "update " + tablename + " set cgsqbh=? where requestid=?";
                    rs.executeUpdate(sql, msgText2, requestid);
                } else {
                    for (int i = 0; i < returnList.size(); i++) {
                        String msgType2 = returnList.getJSONObject(i).getString("MSG_TYPE");
                        if ("E".equalsIgnoreCase(msgType2)) {
                            String msgText2 = returnList.getJSONObject(i).getString("MSG_TEXT");
                            message += msgText2 + ";";
                        }
                    }
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaGdzcgzFxmxgzlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}